/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.framework.comparers
{
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import mx.collections.IList;
	
	import sv.collections.IEqualityComparer;
	import sv.reflection.Type;
	
	public class AssertEqualityComparer implements IEqualityComparer
	{
		private var _caseInsensitive:Boolean;
		private var _collectionFailures:Array;
		
		
		public function AssertEqualityComparer( caseInsensitive:Boolean = false )
		{
			_caseInsensitive = caseInsensitive;
			_collectionFailures = new Array();
		}
		
		
		/**
		 * @param a
		 * @param b
		 * @return
		 */
		public function equals( a:Object, b:Object ) : Boolean
		{
			resetFailures();
			
			return objectsEqual(a, b, new Dictionary(true));
		}
		
		/**
		 * @param a
		 * @param b
		 * @return
		 */
		public function referenceEquals( a:Object, b:Object ) : Boolean
		{
			return (a === b) ? true : false;
		}
		
		/**
		 * 
		 */
		private function resetFailures() : void
		{
			_collectionFailures.splice(0);
		}
		
		/**
		 * @param a
		 * @param b
		 * @return
		 */
		private function valueEquals( a:Object, b:Object ) : Boolean
		{
			return (a == b) ? true : false;
		}
		
		/**
		 * @param a
		 * @param b
		 * @return
		 */
		private function objectsEqual( a:Object, b:Object, references:Dictionary ) : Boolean
		{
			// Handle null instances
			if (a == null && b == null)
			{
				return true;
			}
			else if (a == null || b == null)
			{
				return false;
			}
			
			// Ensure type equality
			if (!referenceEquals(Type.getType(a), Type.getType(b)))
			{
				return false;
			}
			
			// Primitive value types
			if (a is Boolean && b is Boolean)
			{
				return numericsEqual(Number(a), Number(b));
			}
			if (a is Number && b is Number)
			{
				return numericsEqual(a as Number, b as Number);
			}
			if (a is String && b is String)
			{
				return stringsEqual(a as String, b as String);
			}
			
			// Coerced value types
			if (a is Date && b is Date)
			{
				return numericsEqual((a as Date).getTime(), (b as Date).getTime());
			}
			if (a is Namespace && b is Namespace)
			{
				return namespacesEqual(a as Namespace, b as Namespace);
			}
			if (a is QName && b is QName)
			{
				return qnamesEqual(a as QName, b as QName);
			}
			
			// TODO: Should these types also be coerced?
			if (a is RegExp && b is RegExp)
			{
				
			}
			if (a is Error && b is Error)
			{
				
			}
			
			// Xml data types
			if (a is XML && b is XML)
			{
				return valueEquals(a, b);
			}
			if (a is XMLList && b is XMLList)
			{
				return valueEquals(a, b);
			}
			
			// Handle equality comparison of recursive objects
			// and prevent stack overflow.
			var aReference:Boolean = references[a];
			var bReference:Boolean = references[b];
			
			if (aReference && bReference)
			{
				return true;
			}
			else if (aReference || bReference)
			{
				return false;
			}
			
			references[a] = true;
			references[b] = true;
			
			// Enumerable list types
			if (a is Array && b is Array)
			{
				return arraysEqual(a as Array, b as Array, references);
			}
			if (a is IList && b is IList)
			{
				return listsEqual(a as IList, b as IList, references);
			}
			if (a is ByteArray && b is ByteArray)
			{
				return byteArraysEqual(a as ByteArray, b as ByteArray);
			}
			
			// Default equals
			return referenceEquals(a, b);
		}
		
		/**
		 * @param a
		 * @param b
		 * @return
		 */
		private function arraysEqual( a:Array, b:Array, references:Dictionary ) : Boolean
		{
			if (referenceEquals(a, b))
			{
				return true;
			}
			else // default equals
			{
				var key:Object;
				
				// Note: Since Arrays can be associative,
				//		 they must be enumerated as objects.
				for (key in a)
				{
					if (b.hasOwnProperty(key))
					{
						if (!objectsEqual(a[key], b[key], references))
						{
							_collectionFailures.unshift(key);
							
							return false;
						}
					}
					else
					{
						_collectionFailures.unshift(key);
						
						return false;
					}
				}
				
				for (key in b)
				{
					if (!a.hasOwnProperty(key))
					{
						_collectionFailures.unshift(key);
						
						return false;
					}
				}
				
				return true;
			}
		}
		
		/**
		 * @param a
		 * @param b
		 * @return
		 */
		private function listsEqual( a:IList, b:IList, references:Dictionary ) : Boolean
		{
			if (referenceEquals(a, b))
			{
				return true;
			}
			else // default equals
			{
				if (a.length != b.length)
				{
					_collectionFailures.unshift( Math.min(a.length, b.length) );
					
					return false;
				}
				else
				{
					var len:int = a.length;
					for (var i:int = 0; i<len; i++)
					{
						if (!objectsEqual(a.getItemAt(i), b.getItemAt(i), references))
						{
							_collectionFailures.unshift(i);
							
							return false;
						}
					}
					
					return true;
				}
			}
		}
		
		/**
		 * @param a
		 * @param b
		 * @return
		 */
		public function byteArraysEqual( a:ByteArray, b:ByteArray ) : Boolean
		{
			if (referenceEquals(a, b))
			{
				return true;
			}
			else // default equals
			{
				if (a.length != b.length)
				{
					return false;
				}
				else
				{
					a.position = 0;
					b.position = 0;
					
					var len:int = a.length;
					for (var i:int = 0; i<len; i++)
					{
						if (!numericsEqual(a.readByte(), b.readByte()))
						{
							return false;
						}
					}
					
					return true;
				}
			}
		}
		
		/**
		 * @param a
		 * @param b
		 * @return
		 */
		private function namespacesEqual( a:Namespace, b:Namespace ) : Boolean
		{
			if (referenceEquals(a.prefix, b.prefix) && referenceEquals(a.uri, b.uri))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		/**
		 * @param a
		 * @param b
		 * @return
		 */
		private function qnamesEqual( a:QName, b:QName ) : Boolean
		{
			if (referenceEquals(a.localName, b.localName) && referenceEquals(a.uri, b.uri))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		/**
		 * @param a
		 * @param b
		 * @return
		 */
		private function numericsEqual( a:Number, b:Number ) : Boolean
		{
			if (referenceEquals(a, b) || isNaN(a) && isNaN(b))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		/**
		 * @param a
		 * @param b
		 * @return
		 */
		private function stringsEqual( a:String, b:String ) : Boolean
		{
			var stringA:String = (_caseInsensitive) ? a.toLowerCase() : a;
			var stringB:String = (_caseInsensitive) ? b.toLowerCase() : b;
			
			return referenceEquals(stringA, stringB);
		}
		
		
		/**
		 * 
		 */
		public function get collectionFailures() : Array
		{
			return _collectionFailures.slice();
		}
		
	}
	
}